<?php
use Phalcon\Cli\Task;
class CommissionTask extends Task
{
    /**
     * 定时拉取佣金
     */
    public function commissionAction(){
        $wOrderSettleDb = new \Common\Models\WOrderSettle();
        $wOrderSettle = $wOrderSettleDb->find(array(
            'conditions'=>'is_update=1 AND user_id<>0 AND commition<>0',
            'limit'=>500
        ));
        foreach ($wOrderSettle as $key=>$value){
            $commission = \Common\Common::self_commition($value->user_id,$value->commition);
            $WOrderCommissionsDb = new \Common\Models\WOrderCommissions();
            $wOrderCommissionsCount = $WOrderCommissionsDb->orderUserCommissions($value->order_id);
            if(count($commission) != $wOrderCommissionsCount){
                if($wOrderCommissionsCount>0){
                    $WOrderCommissionsDb->find(array(
                        'conditions'=>"order_id='{$value->order_id}' AND settlement_type<>0"
                    ))->delete();
                }
            }
            foreach($commission as $ckey=>$cvalue){
                $addCommissionsDb = new \Common\Models\WOrderCommissions();
                $addCommissionsDb = $addCommissionsDb->findFirst(array(
                    'conditions'=>"user_id='{$cvalue['id']}' AND order_id='{$value->order_id}'",
                ));

                if($addCommissionsDb){
                    $addCommissionsDb->money = $cvalue['percent'];
                    $addCommissionsDb->order_status = $value->order_status;
                    $addCommissionsDb->tk_create_time = $value->order_create_time;
                    $addCommissionsDb->tk_paid_time = $value->order_paid_time;
                    $addCommissionsDb->tk_earning_time = $value->order_earning_time;
                    $addCommissionsDb->updated_at = date("Y-m-d H:i:s");
                }else{
                    $addCommissionsDb = new \Common\Models\WOrderCommissions();
                    $addCommissionsDb->order_id = $value->order_id;
                    $addCommissionsDb->user_id = $cvalue['id'];
                    $addCommissionsDb->buyer_id = $cvalue['buyer'];
                    $addCommissionsDb->money = $cvalue['percent'];
                    $addCommissionsDb->money_plus = 0;
                    $addCommissionsDb->order_status = $value->order_status;
                    $addCommissionsDb->commission_type = $cvalue['id'] == $value->user_id ? 1 :2;
                    $addCommissionsDb->is_free = 0;
                    $addCommissionsDb->tk_create_time = $value->order_create_time;
                    $addCommissionsDb->tk_paid_time = $value->order_paid_time;
                    $addCommissionsDb->tk_earning_time = $value->order_earning_time;
                    $addCommissionsDb->is_settlement = 0;
                    $addCommissionsDb->created_at = date("Y-m-d H:i:s");
                    $addCommissionsDb->updated_at = date("Y-m-d H:i:s");
                    $addCommissionsDb->site = $value->site;
                }
                if($addCommissionsDb->save()){
                    var_dump("插入更新成功{$addCommissionsDb->order_id}");
                }

            }

            $value->is_update = 0;
            $value->updated = date("Y-m-d H:i:s");
            $value->save();


        }

    }
}
